# usage: python computeAveragesAndStdErrorsZconstraintData_2sets.py

import sys,string,math

# Avg and SE for vals from force.dat
a = open('analysisSet1/force.dat','r')
b = open('analysisSet2/force.dat','r')
o = open('force.dat','w') # Write file with all three values of the constraint force.
while 1: # Create infinite loop
        line = a.readline() # Read each line - data MUST begin at line 0.
        line2 = b.readline()
        if not line: break # Break reading loop if blank line
        if not line2: break
        words = string.split(line) # Split each line into columns
        words2 = string.split(line2)
        print >> o, words[0], words[1], words2[1] # Print each value
a.close()
b.close()
o.close()

f = open('force.dat','r')
o = open('avgForce.dat','w')
for line in f:
	val = line.split()
	x = float(val[0]); a = float(val[1]); b =  float(val[2]); #Create array? of floating point numbers
	sumy = a + b # Sum values.
	#print >>o, sumy
	avgy = float(sumy/2) # Avgerage values.
	#av = float((a-avgy)**2) #Calculate the square of the distance from the mean for each value
	#bv = float((b-avgy)**2)
	#cv = float((c-avgy)**2)
	sqa = float((a)**(2))
	sqb = float((b)**(2))
	sum2 = float((sqa+sqb))
	sumsq = float(((a+b)**(2))/2)
	#sdy = float(((av+bv+cv)/3)**(1/2.0))
	sdy = float(((sum2-sumsq)/2)**(1/2.0))
	sey = float((sdy)/((2)**(1/2.0)))
	print >>o, x,"\t",avgy,"\t",sey
	#print >>o, x,"\t",round(avgy,3)," \t",round(sey,3) Rounding - if req.
f.close()
o.close()

# Avg vals and SE for diffusion coefficient.
a = open('analysisSet1/D.dat','r')
b = open('analysisSet2/D.dat','r')
o = open('D.dat','w')
while 1:
        line = a.readline()
        line2 = b.readline()
        if not line: break
        if not line2: break
        words = string.split(line)
        words2 = string.split(line2)
        print >> o, words[0], words[1], words2[1]
a.close()
b.close()
o.close()
f = open('D.dat','r')
o = open('avgD.dat','w')
for line in f:
	val = line.split()
	x = float(val[0]); a = float(val[1]); b =  float(val[2]); #Create array? of floating point numbers
	sumy = a + b
	avgy = float(sumy/2)
	sqa = float((a)**(2))
	sqb = float((b)**(2))
	sum2 = float((sqa+sqb))
	sumsq = float(((a+b)**(2))/2)
	sdy = float(((sum2-sumsq)/2)**(1/2.0))
	sey = float((sdy)/((2)**(1/2.0)))
	print >>o, x,"\t",avgy,"\t",sey
f.close()
o.close()

# Avg vals and SE for free energy.
a = open('analysisSet1/G.dat','r')
b = open('analysisSet2/G.dat','r')
o = open('G.dat','w')
while 1:
        line = a.readline()
        line2 = b.readline()
        if not line: break
        if not line2: break
        words = string.split(line)
        words2 = string.split(line2)
        print >> o, words[0], words[1], words2[1]
a.close()
b.close()
o.close()
f = open('G.dat','r')
o = open('avgG.dat','w')
for line in f:
	val = line.split()
	x = float(val[0]); a = float(val[1]); b =  float(val[2]); #Create array? of floating point numbers
	sumy = a + b
	avgy = float(sumy/2)
	sqa = float((a)**(2))
	sqb = float((b)**(2))
	sum2 = float((sqa+sqb))
	sumsq = float(((a+b)**(2))/2)
	sdy = float(((sum2-sumsq)/2)**(1/2.0))
	sey = float((sdy)/((2)**(1/2.0)))
	print >>o, x,"\t",avgy,"\t",sey
f.close()
o.close()

# Avg vals and SE for resistance.
a = open('analysisSet1/R.dat','r')
b = open('analysisSet2/R.dat','r')
o = open('R.dat','w')
while 1:
        line = a.readline()
        line2 = b.readline()
        if not line: break
        if not line2: break
        words = string.split(line)
        words2 = string.split(line2)
        print >> o, words[0], words[1], words2[1]
a.close()
b.close()
o.close()
f = open('R.dat','r')
o = open('avgR.dat','w')
for line in f:
	val = line.split()
	x = float(val[0]); a = float(val[1]); b =  float(val[2]); #Create array? of floating point numbers
	sumy = a + b 
	avgy = float(sumy/2)
	sqa = float((a)**(2))
	sqb = float((b)**(2))
	sum2 = float((sqa+sqb))
	sumsq = float(((a+b)**(2))/2)
	sdy = float(((sum2-sumsq)/2)**(1/2.0))
	sey = float((sdy)/((2)**(1/2.0)))
#	print >>o, x,"\t",avgy,"\t",sey
	print >>o, "% 6.2f  %30.20f  %30.20f" % (x,avgy,sey)
f.close()
o.close()

# Calculate average vals and SE for the permeability coefficient.
f = open('analysisSet1/P','r')
o = open('avgP.dat','w')
while 1:
  line = f.readline()
  if not line:  break
  words = string.split(line)
  if 'Permeability' in line:
    if 'cm' in line:
        a = float(words[4])
f.close()
f = open('analysisSet2/P','r')
while 1:
  line = f.readline()
  if not line:  break
  words = string.split(line)
  if 'Permeability' in line:
    if 'cm' in line:
        b = float(words[4])
f.close()
sum = a + b # Sum values.
avg = sum/2
sqa = float((a)**(2))
sqb = float((b)**(2))
sum2 = float((sqa+sqb))
sumsq = float(((a+b)**(2))/2)
sd = float(((sum2-sumsq)/2)**(1/2.0))
se = float((sd)/((2)**(1/2.0)))
print >>o, "%8.3e"% avg,"%8.3e"% se
f.close()
o.close()



